* Multiproduct Firms, Horizontal Mergers, and International Trade
* Jackie M.L. Chan, Michael Irlacher, and Michael Koch

* This do file generates Appendix Figure A.3. It computes the number of destinations that each product rank enters after a merger, restricting to either destinations that the acquirer did not previous enter, or both the acquirer and target did not previously enter

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_com_product_cn8plus.dta", clear
keep cvrnrA aar cn8plus vrdA_before
keep if vrdA_before!=.
egen rankAB = rank(vrdA_before), by(cvrnrA aar) field
rename vrdA_before vrdA_before_prod
tempfile temprankAB
save `temprankAB.dta', replace

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_com_product_cn8plus.dta", clear
keep cvrnrA aar cn8plus vrdT_before
keep if vrdT_before!=.
egen rankTB = rank(vrdT_before), by(cvrnrA aar) field
rename vrdT_before vrdT_before_prod
tempfile temprankTB
save `temprankTB.dta', replace

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_vars_com_product_cn8plus.dta", clear
keep cvrnrA aar cn8plus vrdA_after
keep if vrdA_after!=.
egen rankAT = rank(vrdA_after), by(cvrnrA aar) field
rename vrdA_after vrdA_after_prod
tempfile temprankAT
save `temprankAT.dta', replace

********************************************************************************

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_all_largestT_1865.dta", clear
* From did_imputation_vars_prepare_jc_all_AT_31102021.do
keep cvrnrA aar
duplicates drop
sort cvrnrA aar
* Number of years
by cvrnrA: gen dup2= _n
tempfile temp_did_step2_dup2
save `temp_did_step2_dup2.dta', replace
collapse (max) dup2, by(cvrnrA)
rename dup maxdup2
tempfile temp_did_step2_maxdup2
save `temp_did_step2_maxdup2.dta', replace

********************************************************************************

*Prepare data for acquirer in "wide" format to be merged with T-data

use "D:\Data\Workdata\703989\fjsd3989\STATA FILES\uhdi_subset.dta", clear
rename cvrnr cvrnr_uhdi

* We only consider exports in a first step
keep if ie==2
sort senr aar 
merge m:1 senr aar using "D:\Data\Workdata\703989\fjsd3989\STATA FILES\se_cvr_yearly.dta" 

* Compare cvrnr_uhdi with cvrnr in se-cvrnr key file
gen temp=0
replace temp=1 if cvrnr == cvrnr_uhdi & _merge==3
tab temp if _merge==3

keep if _merge==3
drop temp cvrnr_uhdi senr _merge

drop enhedskode ie mgd statistikkode vgt

order cvrnr aar vare land
sort cvrnr aar vare land

rename cvrnr cvrnrA

rename vare vareA
rename aar aarA
rename land landA
rename vrd vrdA

gen byte notnumeric=real(vareA)==.
tab notnumeric
drop if notnumeric==1
destring vareA, replace
rename vareA cn8
drop if vrdA<=0

rename aarA year
merge m:1 cn8 year using "D:\Data\Workdata\703989\fjsd3989\concordance\cn8_cn8plus_1993_2018.dta", keep(match) nogen

rename year aarA
collapse (sum) vrdA, by(cvrnrA aarA landA cn8plus)
rename cn8plus cn8plusA

merge m:1 cvrnrA using `temp_did_step2_maxdup2.dta', keep(match) nogen
expand maxdup2
bysort cvrnrA aarA landA cn8plusA: gen dup2 = _n
merge m:1 cvrnrA dup2 using `temp_did_step2_dup2.dta', keep(match) nogen
drop *dup*

gen d_yr = aarA-aar

drop if aar < 1996 | aar > 2015

gen dup = 1
merge m:1 cvrnrA aar dup using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\data\temp_mergers_all_largestT_1865.dta", keep(match) keepusing(cvrnrT) nogen

keep if (aarA>=aar-3) & (aarA<=aar+3)
gen after = (d_yr>0)

rename cn8plusA cn8plus
preserve
keep if after==0
* Products in all years before merger (including year of merger)
collapse (sum) vrdA, by (cvrnrA aar landA cn8plus)
tempfile tempA_long_before
save `tempA_long_before.dta', replace

restore
keep if after==1
* Products in all years after merger
collapse (sum) vrdA, by (cvrnrA aar landA cn8plus)
tempfile tempA_long_after
save `tempA_long_after.dta', replace

use `tempA_long_before.dta', clear
rename vrdA vrdA_before
merge 1:1 cvrnrA aar landA cn8plus using `tempA_long_after.dta', gen(_mergeAT)
rename vrdA vrdA_after

merge m:1 cvrnrA aar cn8plus using `temprankAB.dta', gen(_mergerankAB)
* Can be unmatched because not in VARS in particular year within merger period
egen max_rankAB = max(rankAB), by(cvrnrA aar)
drop if _mergerankAB==2
egen max_mergeAB = max(_mergerankAB), by(cvrnrA aar)

merge m:1 cvrnrA aar cn8plus using `temprankTB.dta', gen(_mergerankTB)
* Can be unmatched because not in VARS in particular year within merger period
egen max_rankTB = max(rankTB), by(cvrnrA aar)
drop if _mergerankTB==2
egen max_mergeTB = max(_mergerankTB), by(cvrnrA aar)

merge m:1 cvrnrA aar cn8plus using `temprankAT.dta', gen(_mergerankAT)
* Can be unmatched because not in VARS in particular year within merger period
egen max_rankAT = max(rankAT), by(cvrnrA aar)
drop if _mergerankAT==2
egen max_mergeAT = max(_mergerankAT), by(cvrnrA aar)

drop if max_mergeAB==1 & max_mergeAT==1

egen svrdA_before_land = sum(vrdA_before), by(cvrnrA aar landA)
egen svrdA_after_land = sum(vrdA_after), by(cvrnrA aar landA)
egen svrdT_before_land = sum(vrdT_before), by(cvrnrA aar landA)

tempfile tempbase
save `tempbase.dta', replace

* Appendix Figure A.3
foreach letter in a b {
	
postfile stats`letter' str32 varname N mean using "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\figure_A3`letter'.dta", replace

use `tempbase.dta', clear

if "`letter'"=="a"{
gen new_dest = (svrdA_before_land==0 & svrdA_after_land>0)
}
if "`letter'"=="b"{
gen new_dest = (svrdA_before_land==0 & svrdT_before_land==0 & svrdA_after_land>0)
}

forval x = 1/10{
	sort rankAB new_dest cvrnrA aar landA vrdA_before
	by rankAB new_dest cvrnrA aar landA: gen dup = _n if rankAB==`x' & vrdA_before!=. & new_dest==1
	gen dup1 = (dup==1)
	egen ndest_rankAB`x'b = sum(dup1), by(cvrnrA aar)
	drop dup dup1
	
	sort rankAB new_dest cvrnrA aar landA  vrdA_after
	by rankAB new_dest cvrnrA aar landA: gen dup = _n if rankAB==`x' & vrdA_after!=. & new_dest==1
	gen dup1 = (dup==1)
	egen ndest_rankAB`x'a = sum(dup1), by(cvrnrA aar)
	drop dup dup1
	
	sort rankAT new_dest cvrnrA aar landA vrdA_before
	by rankAT new_dest cvrnrA aar landA: gen dup = _n if rankAT==`x' & vrdA_before!=. & new_dest==1
	gen dup1 = (dup==1)
	egen ndest_rankAT`x'b = sum(dup1), by(cvrnrA aar)
	drop dup dup1	
	
	sort rankAT new_dest cvrnrA aar landA vrdA_after
	by rankAT new_dest cvrnrA aar landA: gen dup = _n if rankAT==`x' & vrdA_after!=. & new_dest==1
	gen dup1 = (dup==1)
	egen ndest_rankAT`x'a = sum(dup1), by(cvrnrA aar)
	drop dup dup1
}

collapse (mean) ndest_rank* max_rankAB max_rankAT, by(cvrnrA aar)
rename max_rankAB rankAB
rename max_rankAT rankAT

foreach x in a b{
	foreach y in AB AT{
		foreach z in 1{
			replace ndest_rank`y'`z'`x' = . if rank`y'==.
		}
	}
}
	
foreach x in a b{
	foreach y in AB AT{
		forval z = 2/10{
			replace ndest_rank`y'`z'`x' = . if rank`y'==.
			replace ndest_rank`y'`z'`x' = . if rank`y'<`z'
		}
	}
}

sum ndest_rankAT1a if ndest_rankAB1b!=. & ndest_rankAT1a!=. & ndest_rankAB2b!=. & ndest_rankAT2a!=.
post stats`letter' ("ndest_rankAT1a") (r(N)) (r(mean))

forval x = 2/10{

sum ndest_rankAT`x'a if ndest_rankAB`x'b!=. & ndest_rankAT`x'a!=. 
post stats`letter' ("ndest_rankAT`x'a") (r(N)) (r(mean))

}

postclose stats`letter'

}

set scheme s2color
use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\figure_A3a.dta", clear
gen n = _n
graph twoway (bar mean n, barwidth(0.75)), graphregion(color(white)) ytitle("Number of destinations", size(large)) ylabel(,labsize(large)) xtitle("Product rank", size(large)) xlabel(1 2 3 4 5 6 7 8 9 10, labsize(large)) legend(off) xsc(r(1 10)) title("New destinations of acquirer" " ", size(vlarge) color(black))
graph export "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\figure_A3a.pdf", replace mag(100)

use "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\figure_A3b.dta", clear
gen n = _n
graph twoway (bar mean n, barwidth(0.75)), graphregion(color(white)) ytitle("Number of destinations", size(large)) ylabel(0(0.5)2,labsize(large)) xtitle("Product rank", size(large)) xlabel(1 2 3 4 5 6 7 8 9 10, labsize(large)) legend(off) xsc(r(1 10)) ysc(r(0 2)) title("New destinations" " ", size(vlarge) color(black))
graph export "D:\Data\Workdata\703989\FJSJ3989\CIK\replication_restat\output\figure_A3b.pdf", replace mag(100)